package com.nopay.cdxphone.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.zyyoona7.popup.BasePopup;
import com.zyyoona7.popup.YGravity;
import com.nopay.cdxphone.R;

/**
 * 支付方式选择
 * author：jun
 * date：2021/7/16
 */
public class PayMethodPopup extends BasePopup<PayMethodPopup> {

    private TextView mTvOrderTitle;
    private TextView mTvOrderAmount;
    private CheckBox mWePayCheckBox;
    private CheckBox mAliPayCheckBox;

    public static final int PAY_METHOD_WE = 1;
    public static final int PAY_METHOD_ALI = 2;
    private int mPayMethod = PAY_METHOD_WE;

    public PayMethodPopup(Context context) {
        super();
        setContext(context);
    }

    @Override
    protected void initAttributes() {
        setContentView(R.layout.popup_pay_method, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusAndOutsideEnable(false)
                .setBackgroundDimEnable(true)
                .setAnimationStyle(R.style.PayMethodPopupAnim)
                .setYGravity(YGravity.ALIGN_BOTTOM);
    }

    @SuppressLint("CutPasteId")
    @Override
    protected void initViews(View view, PayMethodPopup popup) {
        findViewById(R.id.tv_cancel_pay).setOnClickListener(v -> {
            // TODO 取消
            dismiss();
            if(onCancelListener != null) onCancelListener.cancel();
        });

        mTvOrderTitle = findViewById(R.id.tv_order_title);
        mTvOrderAmount = findViewById(R.id.tv_order_amount);

        mWePayCheckBox = findViewById(R.id.cb_wepay);
        mAliPayCheckBox = findViewById(R.id.cb_alipay);

        mWePayCheckBox.setOnCheckedChangeListener((compoundButton, checked) -> {
            if(checked) {
                mPayMethod = PAY_METHOD_WE;
                mWePayCheckBox.setClickable(false);
                mAliPayCheckBox.setClickable(true);
                mAliPayCheckBox.setChecked(false);
            }
        });

        mAliPayCheckBox.setOnCheckedChangeListener((compoundButton, checked) -> {
            if(checked) {
                mPayMethod = PAY_METHOD_ALI;
                mAliPayCheckBox.setClickable(false);
                mWePayCheckBox.setClickable(true);
                mWePayCheckBox.setChecked(false);
            }
        });

        findViewById(R.id.layout_wepay).setOnClickListener(v -> {
            if(!mWePayCheckBox.isChecked()) mWePayCheckBox.setChecked(true);
        });

        findViewById(R.id.layout_alipay).setOnClickListener(v -> {
            if(!mAliPayCheckBox.isChecked()) mAliPayCheckBox.setChecked(true);
        });

        mWePayCheckBox.setChecked(true);

        findViewById(R.id.tv_pay_now).setOnClickListener(v -> {
            // TODO 立即支付
            dismiss();
            if(onPayListener != null) onPayListener.pay(mPayMethod);
        });
    }

    public PayMethodPopup setOrderTitle(String title) {
        mTvOrderTitle.setText("订单详情：" + title);
        return self();
    }

    public PayMethodPopup setOrderAmount(String amount) {
        mTvOrderAmount.setText("订单金额：" + amount);
        return self();
    }

    private OnCancelListener onCancelListener;

    public PayMethodPopup setOnCancelListener(OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
        return self();
    }

    public interface OnCancelListener {

        void cancel();
    }

    private OnPayListener onPayListener;

    public PayMethodPopup setOnPayListener(OnPayListener onPayListener) {
        this.onPayListener = onPayListener;
        return self();
    }

    public interface OnPayListener {

        void pay(int payMethod);
    }
}
